package me.firstapp.repository.shop.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import me.firstapp.module.shop.ShoppingCartItem;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.shop.ShoppingCartItemRepository;

@Repository
public class ShoppingCartItemRepositoryImpl extends EntityRepositoryImpl<ShoppingCartItem>
		implements ShoppingCartItemRepository {

	@Override
	protected Class<ShoppingCartItem> getPersistentClass() {
		return ShoppingCartItem.class;
	}

	@Override
	public ShoppingCartItem findShoppingCartItem(Long shoppingCartId, Long productId, Long productSkuRelId) {
		StringBuilder sql = new StringBuilder(" FROM ShoppingCartItem bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.shoppingCartId=:shoppingCartId ");
		sql.append(" AND bean.product.id=:productId");
		sql.append(" AND bean.productSkuRelation.id=:productSkuRelId");

		parameters.put("shoppingCartId", shoppingCartId);
		parameters.put("productId", productId);
		parameters.put("productSkuRelId", productSkuRelId);

		List<ShoppingCartItem> results = super.query(ShoppingCartItem.class, sql.toString(), parameters);
		return results.isEmpty() ? null : results.get(0);
	}

	@Override
	public List<ShoppingCartItem> findShoppingCartItemList(Long shoppingCartId) {
		StringBuilder sql = new StringBuilder(" FROM ShoppingCartItem bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.shoppingCartId=:shoppingCartId ");
		parameters.put("shoppingCartId", shoppingCartId);

		return super.query(ShoppingCartItem.class, sql.toString(), parameters);
	}

}
